package alone.redis.queue.redisqueue.service;

import alone.redis.queue.redisqueue.util.RedisUtil;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * 阻塞方式消费者
 */
public class MessageConsumer implements Runnable{

    private static final String keyName = "redis:queue";

    @Override
    public void run() {
        while (true){
            producerMsg();
        }
    }


    private void producerMsg(){
        Jedis jedis = RedisUtil.getJedis();
        List<String> brpop = jedis.blpop(0,keyName);
        System.out.println(Thread.currentThread().getName() + "   " + brpop);
    }

    public static void main(String[] args) {
        MessageConsumer messageConsumer = new MessageConsumer();
        Thread thread1 = new Thread(messageConsumer,"consumer1");
        Thread thread2 = new Thread(messageConsumer,"consumer2");
        Thread thread3 = new Thread(messageConsumer,"consumer3");
        thread1.start();
        thread2.start();
        thread3.start();
    }


}
